#include<func.h>
//客户端向服务端上传文件

typedef struct train_s{
    int dataLength;
    char buf[1000];
} train_t;
int sendfile(char *argv,int sockFd);

int sendfile(char *argv,int sockFd){//一次mmap
    //切词得到filename
    char command[80];
    strcpy(command,argv);
    char *filename=strtok(command," ");
    filename=strtok(NULL," ");
    puts(filename);//暂时输出一下文件名
    
    //小火车把文件名发过去
    train_t t = {20,filename};
    send(sockFd,&t,4+20,MSG_NOSIGNAL);
    
    int fd = open(t.buf,O_RDONLY);//打开filename文件
    struct stat statbuf;
    int ret = fstat(fd,&statbuf);//看一下filename文件的大小
    
    bzero(&t,sizeof(t));
    t.dataLength = sizeof(statbuf.st_size);
    memcpy(t.buf,&statbuf.st_size,t.dataLength);
    send(sockFd,&t,sizeof(off_t)+4, MSG_NOSIGNAL);
    char *p = (char *)mmap(NULL,statbuf.st_size,PROT_READ,MAP_SHARED,fd,0);
    
    send(sockFd,p,statbuf.st_size,MSG_NOSIGNAL);
    
    //发送结束标志
    t.dataLength = 0;
    send(sockFd,&t,4,MSG_NOSIGNAL);
    munmap(p,statbuf.st_size);
    close(fd);
    return 0;
}

